define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            //绑定select元素事件
            require(['bootstrap-datetimepicker'], function () {
                var options = {
                    format: 'YYYY-MM-DD',
                    icons: {
                        time: 'fa fa-clock-o',
                        date: 'fa fa-calendar',
                        up: 'fa fa-chevron-up',
                        down: 'fa fa-chevron-down',
                        previous: 'fa fa-chevron-left',
                        next: 'fa fa-chevron-right',
                        today: 'fa fa-history',
                        clear: 'fa fa-trash',
                        close: 'fa fa-remove'
                    },
                    showTodayButton: true,
                    showClose: true
                };
                $('.datetimepicker').parent().css('position', 'relative');
                $('.datetimepicker').datetimepicker(options);
            });
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "关键词-搜索";};
            // 初始化表格参数配置
            Table.api.init({
                showHeader:true,
                extend: {
                    index_url: 'task_config3/index' + location.search,
                    add_url: 'task_config3/add',
                    edit_url: 'task_config3/edit',
                    del_url: 'task_config3/del',
                    multi_url: 'task_config3/multi',
                    import_url: 'task_config3/import',
                    table: 'task_config',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'Id',
                sortName: 'Id',
                pageSize:15,
                fixedColumns:true,
                commonSearch: false,
                fixedNumber:2,
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'Id', title: __('ID'), operate: false},
                        {
                            field: 'Name',
                            title: __('关键词'),
                            operate: 'LIKE',
                            formatter:function (value,row) {
                                if(row.Id == 88888888){
                                    return '';
                                }else{
                                    return value;
                                }
                            }
                        },
                        // {field: 'paramstype.name', title: __('请求类型type'), operate: 'LIKE'},
                        // {field: 'paramstype.name', title: __('请求类型type'), operate: false, visible: true, addClass: "selectpage", extend: "data-source='params_type' data-field='name' data-primary-key='name'"},
                        {field: 'TargetUrl', title: __('目标地址'), operate: false,
                            // formatter: Table.api.formatter.url,
                            formatter:function (value,row) {
                                if(row.Id == 88888888){
                                    return '';
                                }else{
                                    return value;
                                }
                            }
                        },
                        {
                            field: 'Eigenvalue',
                            title: '排名（最近30天）',
                            operate: false,
                            formatter:function (value,row) {
                                if(row.Id == '88888888'){
                                    var table_1 = '<table class="table table-bordered text-nowrap" style="width: 3000px;"><thead><tr class="green">';
                                    var table_3 = '</tr></thead></table>';
                                    var table_2 = '';
                                    $.each(value,function(index,e){
                                        table_2 += '<th style="width: 100px;"><h5>'+e+'</h5></th>';
                                    });
                                    return table_1+table_2+table_3;
                                }else{
                                    var table_4 = '<table class="table table-bordered text-nowrap" style="width: 3000px;"><tbody id="append_str_11"><tr>';
                                    var table_6 = '</tr></tbody></table>';
                                    var table_5 = '';
                                    $.each(value,function(index,e){
                                        table_5 += '<td style="width: 100px;">'+e+'</td>';
                                    });
                                    return table_4+table_5+table_6;
                                }
                            }
                        },
                        // {field: 'create_time',
                        //     title: '返回数量（日期）',
                        //     operate:'=',
                        //     addclass:'datetimepicker',
                        //     visible: false,
                        //     cellStyle: Controller.api.formatter.css,
                        //     autocomplete:false,
                        //     defaultValue: (new Date()).toLocaleDateString().split('/').join('-')
                        // },
                        // {field: 'create_time',
                        //     title: __('实际执行数量（日期）'),
                        //     formatter: Table.api.formatter.datetime,
                        //     operate:'RANGE',
                        //     addclass:'datetimerange',
                        //     autocomplete:false,
                        //     visible: false,
                        //     datetimeFormat:"YYYY-MM-DD"
                        // },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                css:function(){
                    return{
                        css:{
                            "max-width":"450px !important",
                            "overflow":"hidden",
                            "white-space":"nowrap",
                            "text-overflow":"ellipsis",
                            "display":"block",
                        }
                    };
                }
            }
        }
    };
    return Controller;
});
